package com.cci.kangdao.escalation.service.impl;

import com.cci.kangdao.aop.UserThreadLocal;
import com.cci.kangdao.dao.RolesTDao;
import com.cci.kangdao.dao.SSRepairTDao;
import com.cci.kangdao.dao.WorkOrderTDao;
import com.cci.kangdao.dao.model.CommonReasonT;
import com.cci.kangdao.dao.model.RolesT;
import com.cci.kangdao.dao.model.UserT;
import com.cci.kangdao.dao.model.WorkOrderT;
import com.cci.kangdao.dto.ReasonResponse;
import com.cci.kangdao.escalation.dao.EscalationChannelReasonConfigTDao;
import com.cci.kangdao.escalation.dao.EscalationChannelRoleConfigTDao;
import com.cci.kangdao.escalation.dao.EscalationChannelTDao;
import com.cci.kangdao.escalation.dto.response.GetEscalationSupportResponse;
import com.cci.kangdao.escalation.model.EscalationChannelReasonConfigT;
import com.cci.kangdao.escalation.model.EscalationChannelRoleConfigT;
import com.cci.kangdao.escalation.model.EscalationChannelT;
import com.cci.kangdao.escalation.service.EscalationChannelService;
import com.cci.kangdao.service.CommonReasonService;
import com.cci.kangdao.utilTool.BeanUtils;
import com.cci.kangdao.utilTool.CollectionUtils;
import com.cci.kangdao.utilTool.NumberUtils;
import com.cci.kangdao.utilTool.ParameterTool;
import com.cci.kangdao.utilTool.RequestContextHolder;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author William Du
 * @date 2020/5/14
 */
@Service
public class EscalationChannelServiceImpl implements EscalationChannelService {
    @Resource
    private EscalationChannelTDao escalationChannelTDao;
    @Resource
    private EscalationChannelRoleConfigTDao escalationChannelRoleConfigTDao;
    @Resource
    private EscalationChannelReasonConfigTDao escalationChannelReasonConfigTDao;
    @Resource
    private WorkOrderTDao workOrderTDao;
    @Resource
    private SSRepairTDao ssRepairTDao;
    @Resource
    private RolesTDao rolesTDao;
    @Resource
    private CommonReasonService commonReasonService;

    @Override
    public void getEscalationSupport(Long workOrderId) {
        HttpServletResponse response = RequestContextHolder.getResponse();
        Integer isSupport = 0;
        if(NumberUtils.isPositiveNum(workOrderId)){
            // 如果传输了服务工单编号，直接查询该工单渠道是否支持
            EscalationChannelT channelT = getWorkOrderChannel(workOrderId);
            if(null != channelT){
                isSupport = 1;
            }
        }else{
            // 如果未传输服务工单ID，则通过用户信息查询是否支持该功能；
            UserT userT = UserThreadLocal.get();
            List<Long> channelList = new ArrayList<>();
            if(NumberUtils.isPositiveNum(userT.getCompanyId())){
                // 如果是企业用户
                channelList.add(userT.getCompanyId());
            }else{
                Long serviceStationId = userT.getServiceStationID();
                if(NumberUtils.isPositiveNum(serviceStationId)){
                    //如果是经销商用户，获取授权渠道
                    List<Map<String,Object>>  ssRepairList = ssRepairTDao.getSSRepairTByServiceStationID(serviceStationId);
                    if(CollectionUtils.isNotEmpty(ssRepairList)){
                        for(Map<String,Object> ssRepairMap:ssRepairList){
                            Long companyId = MapUtils.getLong(ssRepairMap,"CompanyID");
                            channelList.add(companyId);
                        }
                    }
                }
            }

            if(CollectionUtils.isNotEmpty(channelList)){
                List<RolesT> rolesTList = rolesTDao.getRoleListByUserID(userT.getId());
                if(CollectionUtils.isNotEmpty(rolesTList)){
                    support : for(Long companyId:channelList){
                        EscalationChannelT channelT = escalationChannelTDao.selectByCompanyId(companyId);
                        if(null != channelT){
                            List<EscalationChannelRoleConfigT> roleConfigTList = escalationChannelRoleConfigTDao.selectListByChannelId(channelT.getId());
                            if(CollectionUtils.isNotEmpty(roleConfigTList)){
                                for(EscalationChannelRoleConfigT channelRoleConfigT:roleConfigTList){
                                    for(RolesT rolesT:rolesTList){
                                        if(channelRoleConfigT.getRoleNo().equals(rolesT.getRolesNo())){
                                            isSupport = 1;
                                            break support;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        GetEscalationSupportResponse supportResponse = new GetEscalationSupportResponse();
        supportResponse.setIsSupport(isSupport);
        ParameterTool.writeSuccessResponse(response,supportResponse);
    }

    @Override
    public void getEscalationReason(String type, String roleNo, Long workOrderId) {
        HttpServletResponse response = RequestContextHolder.getResponse();
        EscalationChannelT escalationChannelT = getWorkOrderChannel(workOrderId);
        List<CommonReasonT> reasonTList = null;
        if(null != escalationChannelT){
            EscalationChannelReasonConfigT reasonConfigT = escalationChannelReasonConfigTDao.selectByCondition(escalationChannelT.getId(),roleNo,Integer.valueOf(type));
            if(null != reasonConfigT){
               reasonTList = commonReasonService.getReasonList(reasonConfigT.getReasonChannel(),reasonConfigT.getReasonCategory(),reasonConfigT.getReasonType());
            }
        }
        Map<String,Object> data = new HashMap<>(1);
        if(CollectionUtils.isNotEmpty(reasonTList)){
            List<ReasonResponse> reasonResponseList = BeanUtils.convertList(reasonTList,ReasonResponse.class);
            data.put("reasonList",reasonResponseList);
        }else{
            data.put("reasonList",new ArrayList<>(0));
        }
        ParameterTool.writeSuccessResponse(response,data);
    }

    private EscalationChannelT getWorkOrderChannel(Long workOrderId){
        WorkOrderT workOrderT = workOrderTDao.getWorkOrderTByOrderId(workOrderId);
        if(null != workOrderT){
            Integer orderChannel = workOrderT.getOrderChannel();
            EscalationChannelT channelT = escalationChannelTDao.selectByCompanyId(Long.valueOf(orderChannel));
            return channelT;
        }
        return null;
    }

}
